/**
 * @(#)StockService.java, 2013-2-13. Copyright 2013 RENREN, Inc. All rights
 *                        reserved. RENREN PROPRIETARY/CONFIDENTIAL. Use is
 *                        subject to license terms.
 */
package com.wennimei.service;

import org.springframework.stereotype.Service;

import com.wennimei.util.HttpUtil;

/**
 * @author shichen.feng@renren-inc.com
 */
@Service
public class StockService {

    public String get(String code) {

        String list = null;
        if (code.contains("上证") || code.contains("深成")) {
            list = code.contains("上证") ? "s_sh000001" : "s_sz399001";
        } else {
            list = code.startsWith("6") ? "sh" + code : "sz" + code;
        }

        String res = HttpUtil.get("http://hq.sinajs.cn/list=" + list);
        res = res.replaceAll("var hq_str_" + list + "=\"", "").replace(" ", "")
                .replaceAll("\";", "");
        String[] a = res.split(",");
        StringBuilder sb = new StringBuilder();
        if (a.length == 6) {
            sb.append(a[0] + "\r\n");
            sb.append("当前点数:" + a[1] + "\r\n");
            sb.append("当前价格:" + a[2] + "\r\n");
            sb.append("涨跌率:" + a[3] + "\r\n");
            sb.append("成交量（手）:" + a[4] + "\r\n");
            sb.append("成交额（万元）:" + a[5] + "\r\n");
        } else {
            sb.append(a[0] + "\r\n");
            sb.append("今日开盘价:" + a[1] + "\r\n");
            sb.append("昨日收盘价:" + a[2] + "\r\n");
            sb.append("当前价格:" + a[3] + "\r\n");
            sb.append("今日最高价:" + a[4] + "\r\n");
            sb.append("今日最低价:" + a[5] + "\r\n");
        }
        return sb.toString();
    }
}
